import os
import shutil

def delete_files_except_build(directory):
    for root, dirs, files in os.walk(directory):
        if len(files) > 0:
            for file in files:
                if not file.endswith('.build') and not file.endswith('.spec') and not file.endswith('_options.txt'):
                    os.remove(os.path.join(root, file))
        if len(dirs) > 0:
            for indir in dirs:
                if indir == '.git' or indir == '.github' or indir == '.gitlab':
                    shutil.rmtree(os.path.join(root, indir))
                else:
                    if indir is not None:
                        delete_files_except_build(os.path.join(root, indir))

# 使用示例
directory = 'E:\dependency-analysis\package-mapping-results\meson_raw_testdata'
delete_files_except_build(directory)